package cn.xuchunh.campusbbs.ui.fragments;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.xuchunh.campusbbs.R;
import cn.xuchunh.campusbbs.adapter.HomePageAdapter;
import cn.xuchunh.campusbbs.base.BaseFragment;
import cn.xuchunh.campusbbs.bean.Forum;
import cn.xuchunh.campusbbs.utils.IntentParamsKey;

/**
 * Created by XuChunH on 2015/10/17.
 */
public class ForumFragment extends TabFragment {

    private Forum forum;

    public ForumFragment() {
    }

    public static ForumFragment getInstance(Forum forum) {
        ForumFragment forumFragment = new ForumFragment();
        Bundle args = new Bundle();
        args.putParcelable(IntentParamsKey.FORUM, forum);
        forumFragment.setArguments(args);
        return forumFragment;
    }

    @Override
    protected PagerAdapter createAdapter() {
        List<BaseFragment> fragmentList = new ArrayList<>();
        fragmentList.add(TopicListFragment.getInstance(TopicListFragment.TYPE_ALL, forum.getId(), 0, 0, null));
        fragmentList.add(TopicListFragment.getInstance(TopicListFragment.TYPE_NEW, forum.getId(), 0, 0, null));
        fragmentList.add(TopicListFragment.getInstance(TopicListFragment.TYPE_ESSENCE, forum.getId(), 0, 0, null));
        fragmentList.add(TopicListFragment.getInstance(TopicListFragment.TYPE_TOP, forum.getId(), 0, 0, null));
        String[] titles = getResources().getStringArray(R.array.forum_page_title);
        if (forum.hasChild()) {
            fragmentList.add(ChildForumFragment.getInstance(forum.getId()));
        } else {
            titles = Arrays.copyOf(titles, titles.length - 1);
        }

        return new HomePageAdapter(getChildFragmentManager(), fragmentList, Arrays.asList(titles));
    }

    @Override
    protected void handleArgs(Bundle args) {
        forum = args.getParcelable(IntentParamsKey.FORUM);
    }
}
